Skip to content

Avoid parsing nil msg.Value#575

Open
smazurov wants to merge 3 commits into
apache:masterfrom
quantummetric:fix-schema-not-accepting-payload
Open

Avoid parsing nil msg.Value#575
smazurov wants to merge 3 commits into
apache:masterfrom
quantummetric:fix-schema-not-accepting-payload

Conversation

@smazurov
Copy link
Copy Markdown

@smazurov smazurov commented Jul 27, 2021

Motivation

Today, you are forced to use internal schema encoding when sending messages to a topic that has a schema defined. If we do message encoding (into bytes) before hand, set that as a payload but leave value empty, the internal schema encoder fails the message since Value isn't set. This change makes it possible to easily skip this logic without resorting to hacking a Schema interface or other things like double encoding.

This change would also make it a lot easier to get a watermill adapter in place because watermill assumes message encoding happens in "userland".

Modifications

A simple check to ensure msg.Value is not nil

Verifying this change

  • Make sure that the change passes the CI checks.

This change is already covered by existing tests

Does this pull request potentially affect one of the following parts:

  • Dependencies (does it add or upgrade a dependency): no
  • The public API: no
  • The schema: maybe
  • The default values of configurations: maybe
  • The wire protocol: no

var schemaPayload []byte
var err error
if p.options.Schema != nil {
if p.options.Schema != nil && msg.Value != nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be checked within in the if statement and then log an error and return?

if p.options.Schema != nil {
  if msg.Value == nil {
    p.log.WithError(err).Errorf("Schema encode message failed nil message")
    return
  }

}

Or should the ecoders be able to handle a nil value and return an error?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the assumption is that if value is null, a payload is already encoded.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that a safe assumption to make? What happens on the consumer side if that assumption does not hold?

I think we should at least add a comment explaining this because from looking at the code it's not obvious to me.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure - great call. Docs added.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more questions. I'm trying to figure out how to best solve this from an api perspective because if this goes in we are changing the api and will have to support it.

It seems like it's possible to pass a custom implementation of a schema is there a reason this can't solve the issue?

Why is app encoding the message before hand?

If the app is bypassing the internal encoder when producing does it need to bypass internal decoder on the consumer side as well?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you can always bypass it on the consumer side if you don't call it and just read the Payload. You could implement a custom schema, maybe, but it would still be "hacky" for the use case I am proposing which is as a watermill adapter. Their interface requires payload to be a []byte, so we would have to set that to nil, then add the actual value to an untyped property.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the intention is to bypass the schema encoding/decoding why set the schema on the producer and or topic?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still want to take advantage of pushing schema to pulsar topic for schema validation

@wolfstudy wolfstudy added this to the 0.7.0 milestone Jul 29, 2021
@wolfstudy wolfstudy modified the milestones: 0.7.0, v0.8.0 Nov 1, 2021
@wolfstudy wolfstudy modified the milestones: v0.8.0, 0.9.0 Feb 16, 2022
@freeznet freeznet modified the milestones: v0.9.0, v0.10.0 Jul 4, 2022
@RobertIndie RobertIndie modified the milestones: v0.10.0, v0.11.0 Mar 27, 2023
@RobertIndie RobertIndie modified the milestones: v0.11.0, v0.12.0 Jul 4, 2023
@RobertIndie RobertIndie modified the milestones: v0.12.0, v0.13.0 Jan 10, 2024
@RobertIndie RobertIndie modified the milestones: v0.13.0, v0.14.0 Jul 15, 2024
@RobertIndie RobertIndie modified the milestones: v0.14.0, v0.15.0 Oct 8, 2024
@RobertIndie RobertIndie modified the milestones: v0.15.0, v0.16.0 May 15, 2025
@RobertIndie RobertIndie modified the milestones: v0.16.0, v0.17.0 Jul 29, 2025
@RobertIndie RobertIndie modified the milestones: v0.17.0, v0.18.0 Oct 23, 2025
@RobertIndie RobertIndie removed this from the v0.18.0 milestone Dec 1, 2025
@RobertIndie RobertIndie added this to the v0.19.0 milestone Dec 1, 2025
@RobertIndie RobertIndie modified the milestones: v0.19.0, 0.20.0 Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants